package com.ttai.presenter.activity;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ttai.model.net.AccountBean;
import com.ttai.model.net.ArrayInfo;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BaseArrayPresenter;
import com.ttai.ui.activity.ResetKey;
import com.ttai.untils.AesAndroid;
import com.ttai.untils.Constant;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class GetaccountPresenter extends BaseArrayPresenter{

    ResetKey resetKey;
    private String parmJson;

    public GetaccountPresenter(ResetKey resetKey) {
        this.resetKey = resetKey;
    }

    private ArrayList<AccountBean> accountBeansList;




    @Override
    protected void parseArray(JsonArray data) {
//        System.out.println("json"+data.toString());
        Gson gson = new Gson();
        String mJson = data.toString();
        accountBeansList = gson.fromJson(mJson,new TypeToken<ArrayList<AccountBean>>(){}.getType());
        List<HashMap<String,Object>> jsonHashMap = new ArrayList<>();
        for (int i = 0; i < accountBeansList.size(); i++) {
            String number = accountBeansList.get(i).getAccount();
            String password = accountBeansList.get(i).getPassWord();
            String name = accountBeansList.get(i).getAccountName();
            int encryption = accountBeansList.get(i).getEncryption();
            System.out.println("miyao:"+resetKey.getMiYao()+" number:"+number+" password:"+password);
            String xorAcount = AesAndroid.decrypt(resetKey.getMiYao(), Constant.unicode2String(number));
            String xorPassword = AesAndroid.decrypt(resetKey.getMiYao(), Constant.unicode2String(password));
            System.out.println("newMiyao:"+resetKey.getNewMiYao());
            String lastNumber = Constant.string2Unicode(AesAndroid.encrypt(resetKey.getNewMiYao(),xorAcount));
            String lastPassword = Constant.string2Unicode(AesAndroid.encrypt(resetKey.getNewMiYao(),xorPassword));
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("account",lastNumber);
            hashMap.put("accountName",name);
            hashMap.put("passWord",lastPassword);
            jsonHashMap.add(hashMap);
        }
        HashMap<String,Object> param = new HashMap<>();
        param.put("data",jsonHashMap);
        parmJson = gson.toJson(param).toString();
        Log.i("123", "parseArray: "+ parmJson);

        resetKey.getJson(parmJson);

    }



    @Override
    protected void showError(String message) {
        Toast.makeText(resetKey,message,Toast.LENGTH_SHORT).show();
    }
    public void getAccount(String token){
        Call<ArrayInfo> accountInfo = responseInfoApi.taiaccount(token);
        accountInfo.enqueue(new CallBackArrayAdapter());
    }
}
